#!/bin/bash

set -eu

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
source $SNAP/actions/common/utils.sh

if [ -e ${SNAP_DATA}/var/lock/clustered.lock ]
then
  echo "This MicroK8s deployment is acting as a node in a cluster. Please use the microk8s kubectl on the master."
  exit 0
fi

exit_if_stopped

if echo "$*" | grep -v -q -- '--kubeconfig'; then
  exit_if_no_permissions
fi

if [ -e $SNAP_DATA/args/kubectl-env ]
then
  source $SNAP_DATA/args/kubectl-env
fi

declare -a args="($(cat $SNAP_DATA/args/kubectl))"
if [ -n "${args[@]-}" ]
then
  "${SNAP}/kubectl" "${args[@]}" "$@"
else
  "${SNAP}/kubectl" "$@"
fi
